const accessToken = null;
const verifyToken = null;
const v2Header = {
  "content-type": "application/x-www-form-urlencoded"
};
const v4Header = {
  "content-type": "application/json"
};

export const getAccessToken = () => {
  return new Promise((resolve, reject) => {
    uni.request({
      url: '/baiduApi/oauth/2.0/token',
      data: {
        grant_type: 'client_credentials',
        client_id: 'vu2GSb9hkj2mLTU6dfasHR0S',
        client_secret: '2CLnhjEsQAYimrTGOd4ttjxZv3icuH2L'
      },
      method: 'POST',
      header: v2Header,
      success(res) {
        const accessToken = res.data.access_token;
        resolve(accessToken)
      },
      fail(err) {
        reject(err)
      }
    })
  })
}

export const getVerifyToken = (accessToken) => {
  return new Promise((resolve, reject) => {
    uni.request({
      url: '/baiduApi/rpc/2.0/brain/solution/faceprint/verifyToken/generate?access_token=' +
        accessToken,
      data: {
        'plan_id': 15128
      },
      method: 'POST',
      header: v4Header,
      success(res) {
        const verifyToken = res.data.result.verify_token;
        resolve(verifyToken)
      },
      fail(err) {
        reject(err)
      }
    })
  })
}

export const idCardOCR = (imageBase64, accessToken) => {
  return new Promise((resolve, reject) => {
    uni.request({
      url: '/baiduApi/rest/2.0/ocr/v1/idcard?access_token=' + accessToken,
      data: {
        'id_card_side': 'front',
        'detect_photo': true,
        'image': imageBase64
      },
      method: 'POST',
      header: v2Header,
      success(res) {
        resolve(res)
      },
      fail(err) {
        reject(err)
      }
    })
  })
}

export const idCardOCRBack = (imageBase64, accessToken) => {
  return new Promise((resolve, reject) => {
    uni.request({
      url: '/baiduApi/rest/2.0/ocr/v1/idcard?access_token=' + accessToken,
      data: {
        'id_card_side': 'back',
        'image': imageBase64
      },
      method: 'POST',
      header: v2Header,
      success(res) {
        resolve(res)
      },
      fail(err) {
        reject(err)
      }
    })
  })
}

export const bankCardOCR = (imageBase64, accessToken) => {
  return new Promise((resolve, reject) => {
    uni.request({
      url: '/baiduApi/rest/2.0/ocr/v1/bankcard?access_token=' + accessToken,
      data: {
        'image': imageBase64
      },
      method: 'POST',
      header: v2Header,
      success(res) {
        resolve(res)
      },
      fail(err) {
        reject(err)
      }
    })
  })
}


export const updateImageToRepository = (imageBase64, accessToken, verifyToken) => {
  return new Promise((resolve, reject) => {
    uni.request({
      url: '/baiduApi/rpc/2.0/brain/solution/faceprint/uploadMatchImage?access_token=' +
        accessToken,
      data: {
        'verify_token': verifyToken,
        'image': imageBase64
      },
      method: 'POST',
      header: v4Header,
      success(res) {
        resolve(res)
      },
      fail(err) {
        reject(err)
      }
    })
  })
}

export const realNameAuth=(verifyToken, successUrl, failedUrl)=>{
  // window.open(`https://brain.baidu.com/face/print/?token=${verifyToken}&successUrl=${successUrl}&failedUrl=${failedUrl}`);
  window.location.href = `https://brain.baidu.com/face/print/?token=${verifyToken}&successUrl=${successUrl}&failedUrl=${failedUrl}`;
}


export const match = (base64Face1, base64Face2, accessToken) => {
  return new Promise((resolve, reject) => {
    uni.request({
      url: '/baiduApi/rest/2.0/face/v3/match?access_token=' + accessToken,
      data: [
		  {
		    'image': base64Face1,
			'image_type': 'BASE64'
		  },
		  {
		    'image': base64Face2,
		  	'image_type': 'BASE64'
		  }
	  ],
      method: 'POST',
      header: v4Header,
      success(res) {
        resolve(res)
      },
      fail(err) {
        reject(err)
      }
    })
  })
}


export const resultNameAuth = (verifyToken, accessToken) => {
  return new Promise((resolve, reject) => {
    uni.request({
      url: '/baiduApi/rpc/2.0/brain/solution/faceprint/result/media/query?access_token=' + accessToken,
      data: {
		    'verify_token': verifyToken
		  },
      method: 'POST',
      header: v4Header,
      success(res) {
        resolve(res)
      },
      fail(err) {
        reject(err)
      }
    })
  })
}